import java.util.Arrays;

public class LC611 {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int length = nums.length;
        if (length < 3) {
            return 0;
        }
        int result = 0;
        for (int a = 0; a < length - 2; a++) {
            int c = length - 1;
            int b = length - 2;
            int first = nums[a];
            while (c > a + 1) {
                if (first + nums[b] > nums[c] && b > a) {
                    result++;
                    b--;
                } else {
                    c--;
                    b=c-1;
                }
            }
        }
        return result;
    }
}
